package com.two.service.tui.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.two.common.entity.AliPay;
import com.two.common.entity.Tuimoney;
import com.two.common.entity.User;
import com.two.common.entity.UserAndPermissions;
import com.two.common.util.CommonResult;
import com.two.service.tui.OpenFeign.OrderFeign;
import com.two.service.tui.service.TuiService;
import com.two.service.tui.vo.TuiMoneyVo;
import com.two.service.tui.vo.TuiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("tui/tui")
public class TuiController {
    @Autowired
    private TuiService tuiService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OrderFeign orderFeign;

    //查询当前店所有退款详情
    @PostMapping("tuimoney")
    public CommonResult Tuimoney(String token, Integer currentPage, Integer pageSize, @RequestBody TuiVo tuiVo){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson=new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer rid = user.getRid();
      return  tuiService.findAll(rid,currentPage,pageSize,tuiVo);
    }

    //退款通过按钮
    @PostMapping("pass")
    public CommonResult pass(Integer id){
         return  tuiService.update(id);
    }

    //通过后退款
    @PostMapping("return")
    public CommonResult back (@RequestBody TuiMoneyVo tuiMoneyVo){
        System.out.println("===============jjj===================");
        AliPay alipay=new AliPay();
        alipay.setAlipayTraceNo(tuiMoneyVo.getAlipayNo());
        alipay.setSubject(tuiMoneyVo.getOrdersid());
        alipay.setTotalAmount(tuiMoneyVo.getDishTotalPrice());
        alipay.setTraceNo(tuiMoneyVo.getOrdersid());
        System.out.println("!!!!!!"+alipay);
        return orderFeign.returnPay(alipay);
    }

    //申请退款驳回
    @PostMapping("bohui")
    public CommonResult bohui(Integer id, @RequestBody TuiMoneyVo tuiMoneyVo){
       return tuiService.xiu(id,tuiMoneyVo);

    }
}
